package com.zlzlib.libindexpy.widget;

import android.content.Context;
import android.util.AttributeSet;


import com.zlzlib.libindex.bean.BaseIndexBean;
import com.zlzlib.libindex.widget.BaseIndexBar;
import com.zlzlib.libindexpy.bean.BasePyIndexBean;
import com.zlzlib.libindexpy.helper.PyDataHelper;
import com.zlzlib.libindexpy.helper.PyDataHelperImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zlz on 2019/11/4.
 * 拼音检索RecyclerView 的view
 */
public class PyIndexBar extends BaseIndexBar {

    private List<? extends BasePyIndexBean> mSourceData;
    //源数据 是否需要排序
    private boolean isSourceNeedSort = true;
    //是否添加数字的检索
    private boolean isNeedNumber = false;
    private PyDataHelper dataHelper;

    public PyIndexBar(Context context) {
        this(context, null);
    }

    public PyIndexBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PyIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        dataHelper = new PyDataHelperImpl();
    }

    @Override
    protected String[] defDataString() {
        if (isNeedNumber) {
            return new String[]{
                    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
                    "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"
            };
        } else {
            return new String[]{
                    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
                    "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"
            };
        }
    }

    @Override
    protected List<? extends BaseIndexBean> sourceData() {
        return mSourceData;
    }

    /**
     * 设置数据
     *
     * @param mSourceData
     * @return
     */
    public BaseIndexBar setSourceData(List<? extends BasePyIndexBean> mSourceData) {
        this.mSourceData = mSourceData;
        initSourceData();//对数据源进行初始化
        return this;
    }

    /**
     * 设置是否需要排序
     *
     * @param sourceNeedSort
     */
    public void setSourceNeedSort(boolean sourceNeedSort) {
        isSourceNeedSort = sourceNeedSort;
    }

    /**
     * 是否需要数字排序
     *
     * @param needNumber
     */
    public void setNeedNumber(boolean needNumber) {
        isNeedNumber = needNumber;
        initIndexData();
    }

    /**
     * 设置排序比较的拼音长度
     *
     * @param length 默认为5
     */
    public void setSortChartLength(int length) {
        dataHelper.setSortLength(length);
    }

    /**
     * 初始化原始数据源，并取出索引数据源
     *
     * @return
     */
    private void initSourceData() {
        if (null == mSourceData || mSourceData.isEmpty()) {
            return;
        }
        if (isSourceNeedSort) {
            //排序sourceData
            dataHelper.sortSourceData(mSourceData, isNeedNumber);
        } else {
            //汉语->拼音
            dataHelper.convert(mSourceData);
            //拼音->tag
            dataHelper.fillIndexTag(mSourceData, isNeedNumber);
        }
        if (isNeedRealIndex) {
            mIndexData = new ArrayList<>();
            dataHelper.getSortedIndexData(mSourceData, mIndexData);
            computeGapHeight();
        }
    }
}
